package com.premize.grco.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.premize.grco.dataaccess.sessionFactory.HibernateSessionFactory;
import com.premize.grco.movil.GrcoPedido;


/**
 * A data access object (DAO) providing persistence and search support for
 * GrcoPedido entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class GrcoPedidoDAO implements IGrcoPedidoDAO {
    private static final Log log = LogFactory.getLog(GrcoPedidoDAO.class);

    // property constants
    //public static final Long  CONSPEDIDO = "consPedido";
    public static final String CONSPEDIDO = "consPedido";

    //public static final Long  ESTADO = "estado";
    public static final String ESTADO = "estado";

    //public static final Date  FECHAPEDIDO = "fechaPedido";
    public static final String FECHAPEDIDO = "fechaPedido";

    //public static final Double  TOTALPEDIDO = "totalPedido";
    public static final String TOTALPEDIDO = "totalPedido";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            GrcoPedido Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(GrcoPedido instance) {
        log.debug("saving GrcoPedido instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            GrcoPedido Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(GrcoPedido instance) {
        log.debug("deleting GrcoPedido instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            GrcoPedido Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(GrcoPedido instance) {
        log.debug("updating GrcoPedido instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public GrcoPedido findById(Long id) {
        log.debug("finding GrcoPedido instance with id: " + id);

        try {
            GrcoPedido instance = (GrcoPedido) getSession()
                                                   .get(GrcoPedido.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding GrcoPedido failed", re);
            throw re;
        }
    }

    public List<GrcoPedido> findByExample(GrcoPedido instance) {
        log.debug("finding GrcoPedido instance by example");

        try {
            List results = getSession().createCriteria("GrcoPedido")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  GrcoPedido entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< GrcoPedido> found by query
        */
    public List<GrcoPedido> findByProperty(String propertyName, Object value) {
        log.debug("finding GrcoPedido instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from GrcoPedido as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<GrcoPedido> findByConsPedido(Object consPedido) {
        return findByProperty(CONSPEDIDO, consPedido);
    }

    public List<GrcoPedido> findByEstado(Object estado) {
        return findByProperty(ESTADO, estado);
    }

    public List<GrcoPedido> findByFechaPedido(Object fechaPedido) {
        return findByProperty(FECHAPEDIDO, fechaPedido);
    }

    public List<GrcoPedido> findByTotalPedido(Object totalPedido) {
        return findByProperty(TOTALPEDIDO, totalPedido);
    }

    /**
    * Find all GrcoPedido entities.
    *
    * @return List<GrcoPedido> all GrcoPedido instances
    */
    public List<GrcoPedido> findAll() {
        log.debug("finding all GrcoPedido instances");

        try {
            String queryString = "from GrcoPedido";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<GrcoPedido> findByCriteria(String whereCondition) {
        log.debug("finding GrcoPedido " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from GrcoPedido model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<GrcoPedido> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in GrcoPedido failed", re);
            throw re;
        }
    }

    public List<GrcoPedido> findPageGrcoPedido(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding GrcoPedido findPageGrcoPedido");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from GrcoPedido model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from GrcoPedido model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberGrcoPedido() {
        log.debug("finding GrcoPedido count");

        try {
            String queryString = "select count(*) from GrcoPedido model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
